package per.lance.easytomcat.database;

import per.lance.easytomcat.bean.Book;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class DB {
    public ConcurrentMap<String, Book> books = new ConcurrentHashMap<String, Book>();

    public static DB db = null;

    public DB() {
        for (int i = 1; i <= 20; i++) {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            Book book = new Book();
            book.setId(uuid);
            book.setName("name" + i);
            book.setPrice(i * (0.01 + i));
            books.put(uuid, book);
        }
    }

    public ConcurrentMap<String, Book> getBooks() {
        return books;
    }

    public void setBooks(ConcurrentMap<String, Book> books) {
        this.books = books;
    }

    public static synchronized DB getInstance() {
        if (db == null) {
            db = new DB();
        }
        return db;
    }
}
